// pages/goodsDetail/goodsDetail.js
import {
  getProductDetail,
  shopingCart
} from '../../api/apis.js'
let WxParse = require('../../plugin/wxParse/wxParse.js');
const app = getApp()
Page({
  data: {
    showModalStatus: false,
    productDetail: {},
    count: 1,
    reduceBtn: false,
    indicatorDots: true,
    autoplay: true,
    proList: ['正品保障', '七天包退', '极速发货'],
    sizeChecked: {},
    modalBtnText: '',
    buyORadd: 1
  },
  addCartClick: function(e) {
    this.setData({
      showModalStatus: true,
      modalBtnText: e.currentTarget.dataset.text,
      buyORadd: e.currentTarget.dataset.next
    })
  },

  onLoad: function(options) {
    this.setData({
      mid: options.did
    })
    this._getProductDetail()
  },
  _getProductDetail() {
    getProductDetail({
      data: {
        id: this.data.mid
      },
      success: res => {
        if (res.state) {
          //整合头部轮播图
          let imgUrls = []
          imgUrls.push(res.data.mainimg)
          res.data.img.forEach((item) => {
            imgUrls.push(item.URL)
          })
          res.data.imgUrls = imgUrls
          //修改详情中图片的最大宽度不超过手机宽度
          // let detail = res.data.detail && res.data.detail.replace(/<img/g, "<img style='max-width:100%'")
          // res.data.content = detail
          let detail = res.data.detail || "" 
          WxParse.wxParse('article', 'html', detail, this, 5)
          //默认选中第一个sku
          res.data.sku[0].selected = true
          //最终赋值
          this.setData({
            productDetail: res.data,
            sizeChecked: res.data.sku[0]
          })
          this.calculate()
        }
      }
    })
  },
  clickCapacity: function(e) {
    let index = e.currentTarget.dataset.index
    let current = "productDetail.sku[" + index + "].selected"
    this.data.productDetail.sku.map((item, index) => {
      item.selected = false
      return item
    })
    this.setData({
      productDetail: this.data.productDetail,
      [current]: true,
      sizeChecked: this.data.productDetail.sku[index]
    })
    this.calculate()
  },
  cancelModal: function() {
    this.setData({
      showModalStatus: false
    })
  },
  addCount: function() {
    let count = this.data.count
    this.setData({
      count: ++count
    })
    this.calculate()
    this.setData({
      reduceBtn: true
    })
  },
  reduceCount: function() {
    let count = this.data.count
    if (count > 1) {
      count--
      this.setData({
        count: count
      })
      this.calculate()
      if (count <= 1) {
        this.setData({
          reduceBtn: false
        })
      }
    }
  },
  //计算价格函数
  calculate() {
    let skus = this.data.productDetail.sku
    let checkedSku = {}
    for (let i = 0; i <= skus.length - 1; i++) {
      if (skus[i].selected) {
        checkedSku = skus[i]
        break
      }
    }
    this.setData({
      total: checkedSku.price * this.data.count
    })
  },
  addCart() {
    if (this.data.buyORadd == 1) {
      wx.showLoading({
        title: '加入中...',
        mask: true
      })
      shopingCart({
        data: {
          openid: app.globalData.openid,
          product_Id: this.data.mid,
          sku_id: this.data.sizeChecked.id,
          variable: this.data.count
        },
        success: res => {
          wx.hideLoading()
          if (res.state) {
            wx.showToast({
              title: '添加成功'
            })
            this.setData({
              showModalStatus: false
            })
          }
        }
      })
    } else if (this.data.buyORadd == 2) {
      let confirmProduct = [
        {
          img: this.data.productDetail.mainimg,
          title: this.data.productDetail.name,
          cpacity: this.data.sizeChecked.title,
          price: this.data.sizeChecked.price,
          count: this.data.count,
          product_Id: this.data.mid,
          sku_id: this.data.sizeChecked.id
        }
      ]
      wx.setStorage({
        key: 'confirmProduct',
        data: confirmProduct,
        success: res => {
          wx.navigateTo({
            url: '../confirm-order/confirm-order',
          })
        }
      })
    }

  },
  /*
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})